﻿namespace AC.ExtendedRenderer.Toolkit.Navigator
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Button))]
    public class KryptonNavigatorButton : KryptonButton
    {
        private static IPalette _palette;
        private static PaletteRedirect _paletteRedirect;

        public KryptonNavigatorButton()
        {
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new EventHandler(this.GlobalPaletteChanged);
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect = new PaletteRedirect(_palette);
            this.AutoSize = false;
            base.Values.ExtraText = null;
            base.Values.Text = null;
            base.Values.Image = null;
            base.Values.ImageStates.ImageCheckedNormal = null;
            base.Values.ImageStates.ImageCheckedPressed = null;
            base.Values.ImageStates.ImageCheckedTracking = null;
            base.ButtonStyle = ButtonStyle.Standalone;
            base.StateNormal.Back.Color1 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
            base.StateNormal.Back.Color2 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
            base.StateNormal.Back.ColorStyle = PaletteColorStyle.Solid;
            base.StateNormal.Border.Color1 = _palette.ColorTable.ToolStripBorder;
            base.StateCommon.Border.Rounding = 0;
            base.Size = new Size(0x17, 0x17);
            base.StateCommon.Content.ShortText.Color1 = _palette.ColorTable.StatusStripText;
            base.StateCommon.Content.ShortText.TextH = PaletteRelativeAlign.Center;
            base.StateCommon.Content.ShortText.TextV = PaletteRelativeAlign.Center;
            base.StateCommon.Content.ShortText.Font = new Font("Marlett", 11f, FontStyle.Regular, GraphicsUnit.Point, 2);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_palette != null)
                {
                    _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                    _palette = null;
                }
                KryptonManager.GlobalPaletteChanged -= new EventHandler(this.OnGlobalPaletteChanged);
            }
            base.Dispose(disposing);
        }

        private void GlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
            {
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect.Target = _palette;
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                base.StateNormal.Back.Color1 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
                base.StateNormal.Back.Color2 = _palette.ColorTable.ToolStripContentPanelGradientEnd;
                base.StateNormal.Border.Color1 = _palette.ColorTable.ToolStripBorder;
                base.StateCommon.Content.ShortText.Color1 = _palette.ColorTable.StatusStripText;
            }
            base.Invalidate();
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }
    }
}

